home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiscKit1.7.1 / MiscKit / Temp / MiscTimeAdditions / day-of-week.message next >
Encoding:
Text File  |  1994-11-07  |  3.7 KB  |  94 lines

  1. CUU1@VAXA.NEWCASTLE-POLY.AC.UK (David Hardy) / 12:23 pm  Feb 10, 1992 */
  2. In message <01GG0A5BZP00001MSC@EKU.BITNET>, David Barbee
  3. (<STUBRBEE%bitnet.EKU@edu.CUNY.CUNYVM>) writes:
  4.  
  5. >      I'm looking for an algorithm or source code (preferably pascal) to
  6. > calculate what day of the week any given day is on.  I know that asking
  7. > for ANY date is a little much...but i would like for it to be accurate
  8. > as possible.
  9.  
  10. As several Info-VAXers have noted, library routine LIB$DAY_OF_WEEK will
  11. return the required information on a VMS system. If the program is to run on
  12. several platforms, however, another method is needed. The program below
  13. contains a routine (DOW) which takes the day, month and year (as integers
  14. and with the year containing the century) and returns an integer indicating
  15. the day of the week with Sunday as 0, Monday as 1, etc. The algorithm
  16. doesn't check that the date is reasonable (you can enter 31 2 1992 for
  17. February 31st, 1992, for example) and it doesn't work for dates before the
  18. change to the Gregorian calendar which took place on the 2nd September, 1752
  19. (and made the next date the 14th September). The curious formula
  20.  
  21.         weekday := (260 * month - 19) div 100 + day + year + year div 4 +
  22.                     century div 4 - 2 * century;
  23.  
  24. was devised by the Reverend Zeller and means that the algorithm doesn't need
  25. to use a look-up table of month lengths. The algorithm also adjusts the year
  26. to start on March 1st as this means that February 29th falls at the end of a
  27. year and doesn't require special-case handling.
  28.  
  29. I converted the algorithm from BBC BASIC to Pascal and the original program
  30. is listed in the book "132 Short Programs for the Mathematics Classroom" by
  31. The Mathematical Association which is published by Stanley Thornes
  32. (Publishers) Ltd (ISBN 0-85950-556-1). I have no further reference for the
  33. algorithm itself.
  34.  
  35. The DOW routine should work if transferred to another program but you should
  36. satisfy yourself that it does what you want - I take no responsibility for
  37. errors should there be any.
  38.  
  39. ============================================================================
  40.  
  41. program weekday(input,output);
  42.  
  43. var
  44.     d, m, y : integer;
  45.  
  46. function dow(day, month, year : integer) : integer;
  47.  
  48.     var
  49.         century, weekday : integer;
  50.  
  51.     begin
  52.         if (month = 1) or (month = 2) then
  53.             begin
  54.                 month := month + 12;
  55.                 year := year - 1;
  56.             end;
  57.         month := month - 2;
  58.         century := year div 100;
  59.         year := year mod 100;
  60.         weekday := (260 * month - 19) div 100 + day + year + year div 4 +
  61.                     century div 4 - 2 * century;
  62.         dow := weekday mod 7;
  63.     end;
  64.  
  65. begin
  66.     repeat
  67.         write('Enter date in form d m y (include century) or 0 0 0 to end ');
  68.         readln(d,m,y);
  69.         if (d > 0) then
  70.             case dow(d,m,y) of
  71.                 0 : writeln('Sunday');
  72.                 1 : writeln('Monday');
  73.                 2 : writeln('Tuesday');
  74.                 3 : writeln('Wednesday');
  75.                 4 : writeln('Thursday');
  76.                 5 : writeln('Friday');
  77.                 6 : writeln('Saturday');
  78.             end;
  79.     until d = 0;
  80. end.
  81.  
  82. ============================================================================
  83.  
  84. +----------------------------------------------------------------+
  85. | David Hardy                                                    |
  86. | Computer Unit, Newcastle Polytechnic, Northumberland Building, |
  87. | Ellison Place, Newcastle upon Tyne, NE1 8ST, U.K.              |
  88. +----------------------------------------------------------------+
  89. | JANET: DAVID.HARDY@NPY.AC.UK                                   |
  90. +----------------------------------------------------------------+
  91.  
  92. /* ---------- */
  93.  
  94.